package com.ax.base.drugproducer.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.common.utils.PageUtils;
import com.ax.base.drugproducer.domain.BaseProducer;
import com.ax.base.drugproducer.service.DrugProducerService;

@Controller
@RequestMapping("/sys/drugProducer")
public class DrugProducerController {

	@Autowired
	DrugProducerService drugProducerService;
	
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String drugproducer(Model model) {
		return "drugproducer/drugproducer/drugproducer";
	}
	
	/**
	 * 药品厂家
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@RequestMapping("/queryProducer")
	@ResponseBody
	public List<BaseProducer> queryProducer(@RequestParam Map<String,Object> params) {
	//	PageUtils producers = this.drugProducerService.queryDrugproducerSer(params);
		List<BaseProducer> BaseProducer = this.drugProducerService.queryDrugproducerSer(params);
		return BaseProducer;
	  }
	
	/**
	 * 药品厂家增加(模块:药品厂家)*跳转增加页面
	 * 
	 * @param 无
	 *           
	 * @return HTML
	 */
	@GetMapping("/add")
	String add(Model model) {
		return "drugproducer/drugproducer/add";
	}
	
	/**
	 * 药品厂家增加(模块:药品厂家)
	 * 
	 * @param BaseProducer baseProducer
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertProducer")
	@ResponseBody
	public AjaxRes insertBaseDrug(AjaxRes ajaxRes,BaseProducer baseProducer){
		try {
			this.drugProducerService.insertBaseDrugSer(baseProducer);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 药品厂家修改(模块:药品厂家)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return HTML
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("producer",drugProducerService.getProducerSer(id));
		return "drugproducer/drugproducer/edit";
	}
	
	/**
	 * 药品厂家修改(模块:药品厂家)
	 * 
	 * @param BaseProducer baseProducer
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateProducer")
	@ResponseBody
	public AjaxRes updateProducer(AjaxRes ajaxRes,BaseProducer baseProducer){
		try {
			this.drugProducerService.updateBaseDrugSer(baseProducer);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 验证信用代码是否重复(新增)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@PostMapping("/validate")
	@ResponseBody
	public boolean validate(@RequestParam Map<String,Object> params){
		return !drugProducerService.validateSer(params);
	}
	
	/**
	 * 验证信用代码是否重复(修改)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@PostMapping("/updateValidate")
	@ResponseBody
	public boolean updateValidate(String username){
		return !drugProducerService.updateValidateSer(username);
	}
	
}
